import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
//import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.Select;


public class KDF3 {
	
	@Test
	public void myDriver() throws Exception{
		String[][] xTC, xTS;
		String vKW, vXPath, vTestData;
		//WebDriver myD = new FirefoxDriver();
		WebDriver myD = new InternetExplorerDriver();
		// Read the TC and TS
		xTC = readXL("C:\\Selenium\\ScreenCast_Download\\Aug16thBatch\\KDFAutomation.xls", "Test Cases");
		xTS = readXL("C:\\Selenium\\ScreenCast_Download\\Aug16thBatch\\KDFAutomation.xls", "Test Steps");
		int vRows = xTC.length;
		int vCols = xTC[0].length;
		for (int i=1; i<xTC.length ; i++) {// Go to each Row in TC
			if (xTC[i][3].equalsIgnoreCase("Y")) { //identify which TC to execute
				for (int k=1;k<xTS.length; k++){// Go to every TS 				 
					if (xTC[i][0].equalsIgnoreCase(xTS[k][1])) { // see if TCID's match
						
						vKW = xTS[k][4];
						vXPath = xTS[k][5];
						vTestData = xTS[k][6];
						
						// Call the corresponding Keyword Function
						if (vKW.equalsIgnoreCase("enterText")) {
							enterText(myD, vXPath, vTestData);
						}
						if (vKW.equalsIgnoreCase("navigateBrowser")) {
							navigateBrowser(myD,vTestData);
						}
						if (vKW.equalsIgnoreCase("clickElement")) {
							clickElement(myD,vXPath);
						}
						if (vKW.equalsIgnoreCase("readText")) {
							System.out.println("Text here is " + readText(myD,vXPath));
						}
						if (vKW.equalsIgnoreCase("verifyText")) {
							System.out.println("Test Step is a " + verifyText(myD,vXPath, vTestData));
						}
						
						
						// Capture o/p results
					}
				}
			}

		}
		myD.quit();

	}

	
	public void enterText(WebDriver driver, String xPath, String data){
		//		Purpose: Enter Text into a edit field
		//		I/P:driver, xPath, Data
		//		O/P:Nil
		//		CreatedBy:SelA16 Batch
		//		When:9/11/12
		//		EditedBy:Karthik
		//		When:9/11/12
		driver.findElement(By.xpath(xPath)).clear();
		driver.findElement(By.xpath(xPath)).sendKeys(data);
	}
	
	public String readText(WebDriver driver, String xPath){
		//		Purpose: Read Text from a edit field
		//		I/P:driver, xPath
		//		O/P:Data
		//		CreatedBy:SelA16 Batch
		//		When:9/11/12
		//		EditedBy:Karthik
		//		When:9/11/12
		return driver.findElement(By.xpath(xPath)).getText();
	}
	
	public String verifyText(WebDriver driver, String xPath, String sText){
		//		Purpose: Verify Text from a edit field
		//		I/P:driver, xPath
		//		O/P:Data
		//		CreatedBy:SelA16 Batch
		//		When:9/11/12
		//		EditedBy:Karthik
		//		When:9/11/12
		if (sText.equalsIgnoreCase(driver.findElement(By.xpath(xPath)).getText())){
			return "Pass";
		}else {
			return "Fail";
		}
		
	}
	
	public void clickElement(WebDriver driver, String xPath){
		//		Purpose: Click any element on the AUT
		//		I/P:driver, xPath
		//		O/P:Nil
		//		CreatedBy:SelA16 Batch
		//		When:9/11/12
		//		EditedBy:Karthik
		//		When:9/11/12
		driver.findElement(By.xpath(xPath)).click();
		//driver.findElement(By.xpath(xPath)).sendKeys("\n");
	}
	
	public void selectList(WebDriver driver, String xPath, String data){
		//		Purpose: Select from a dropdown list
		//		I/P:driver, xPath, data
		//		O/P:Nil
		//		CreatedBy:SelA16 Batch
		//		When:9/11/12
		//		EditedBy:Karthik
		//		When:9/11/12
		Select myDD = new Select(driver.findElement(By.xpath(xPath)));
		myDD.selectByVisibleText(data);
		myDD = null;
	}
	
	public void checkBox(WebDriver driver, String xPath){
		//		Purpose: Check a checkbox
		//		I/P:driver, xPath, data
		//		O/P:Nil
		//		CreatedBy:SelA16 Batch
		//		When:9/11/12
		//		EditedBy:Karthik
		//		When:9/11/12
		// Is it checked
		if(driver.findElement(By.xpath(xPath)).getAttribute("checked").equalsIgnoreCase("checked")){
			//Then dont do anything
		} else {
			driver.findElement(By.xpath(xPath)).click();
		}
	}

	public void navigateBrowser(WebDriver driver, String data){
		//		Purpose: Navigate a browser to a URL
		//		I/P:driver, data
		//		O/P:Nil
		//		CreatedBy:SelA16 Batch
		//		When:9/11/12
		//		EditedBy:Karthik
		//		When:9/11/12
		// Is it checked
		driver.navigate().to(data);
	}
	// CUSTOM METHODS ARE HERE
	// Method to read XL
	public String[][] readXL(String sPath, String iSheet) throws Exception{
		String[][] xData;                                                                
		File myxl = new File(sPath);                                
		FileInputStream myStream = new FileInputStream(myxl);                                
		HSSFWorkbook myWB = new HSSFWorkbook(myStream);                                
		HSSFSheet mySheet = myWB.getSheet(iSheet);                                 
		int xRows = mySheet.getLastRowNum()+1;                                
		int xCols = mySheet.getRow(0).getLastCellNum();                                
		xData = new String[xRows][xCols];        
		for (int i = 0; i < xRows; i++) {                           
				HSSFRow row = mySheet.getRow(i);
				for (int j = 0; j < xCols; j++) {                               
					HSSFCell cell = row.getCell(j);                               
					String value = cellToString(cell);                               
					xData[i][j] = value;                               
					}        
				}                                      
		return xData;
	}
	
	// Method to write into an XL
	public void writeXL(String sPath, String iSheet, String[][] xData) throws Exception{

    	File outFile = new File(sPath);
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet osheet = wb.createSheet(iSheet);
        int xR_TS = xData.length;
        int xC_TS = xData[0].length;
    	for (int myrow = 0; myrow < xR_TS; myrow++) {
	        HSSFRow row = osheet.createRow(myrow);
	        for (int mycol = 0; mycol < xC_TS; mycol++) {
	        	HSSFCell cell = row.createCell(mycol);
	        	cell.setCellType(HSSFCell.CELL_TYPE_STRING);
	        	cell.setCellValue(xData[myrow][mycol]);
	        }
	        FileOutputStream fOut = new FileOutputStream(outFile);
	        wb.write(fOut);
	        fOut.flush();
	        fOut.close();
    	}
	}

	
	//Change cell type
	public static String cellToString(HSSFCell cell) { 
		// This function will convert an object of type excel cell to a string value
		int type = cell.getCellType();                        
		Object result;                        
		switch (type) {                            
			case HSSFCell.CELL_TYPE_NUMERIC: //0                                
				result = cell.getNumericCellValue();                                
				break;                            
			case HSSFCell.CELL_TYPE_STRING: //1                                
				result = cell.getStringCellValue();                                
				break;                            
			case HSSFCell.CELL_TYPE_FORMULA: //2                                
				throw new RuntimeException("We can't evaluate formulas in Java");  
			case HSSFCell.CELL_TYPE_BLANK: //3                                
				result = "%";                                
				break;                            
			case HSSFCell.CELL_TYPE_BOOLEAN: //4     
				result = cell.getBooleanCellValue();       
				break;                            
			case HSSFCell.CELL_TYPE_ERROR: //5       
				throw new RuntimeException ("This cell has an error");    
			default:                  
				throw new RuntimeException("We don't support this cell type: " + type); 
				}                        
		return result.toString();      
		}
}
